package com.ynyyrj.position.controller;

import com.ynyyrj.position.pojo.position;
import com.ynyyrj.position.service.CompanyPosService;
import com.ynyyrj.position.service.PosService;
import com.ynyyrj.position.service.ResumePosService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;


import java.util.Date;
import java.util.List;


@Controller
public class PositionController {

    @Autowired
    PosService posService;

    @Autowired
    ResumePosService resumePosService;

    @Autowired
    CompanyPosService companyPosService;

    @RequestMapping({"/dashboard","/"})
    public String login(){
        return "dashboard";
    }

    @RequestMapping("/list")
    public String SelPos(Model model){
        List<String> position=posService.selectAllPosition();
        System.out.println(position);
        model.addAttribute("pos",position);
        return "emp/list";
    }

    @RequestMapping("/Add")
    public String xg(){
        return "emp/Add";
    }

    @PostMapping("/xz")
    public String xz( String name){
        List<String> list=posService.selectAllPosition();
        boolean x=list.contains(name);
        if(!x){
           Date date1=new Date();
           Date date2=new Date();
           position position=new position();
           position.setName(name);
           position.setTime_add(date1);
           position.setTime_update_last(date2);
           System.out.println(position);
           posService.add(position);
           return "redirect:/list";
        }
        else{
            return "emp/Add";
        }
    }

    @RequestMapping("/AddUpda/{name}")
    public String update(@PathVariable("name") String name,Model model){
        model.addAttribute("s",name);
        int i=posService.selectId(name);
        model.addAttribute("s1",i);
        int x=companyPosService.jsCompany(i);
        int y=resumePosService.jsResume(i);
        if(x+y==0){
            return "emp/Update";
        }
        return "redirect:/list";
    }

    @RequestMapping("/updatesuccess/{aid}")
    public String update1(@PathVariable("aid") int aid,String name){
        Date date1=new Date();
        position position=new position();
        position.setName(name);
        position.setAid(aid);
        position.setTime_update_last(date1);
        posService.update(position);
        System.out.println(position);
        return "redirect:/list";
    }

    @RequestMapping("/cancel")
    public String cancel(){
        return "redirect:/list";
    }

    @RequestMapping("/Del/{name}")
    public String toDel(@PathVariable("name") String name,Model model){
        model.addAttribute("s",name);
        int i= posService.selectId(name);
        int x=companyPosService.jsCompany(i);
        int y=resumePosService.jsResume(i);
        if(x+y==0){
            return "emp/Delete";
        }
        return "redirect:/list";
    }




    @RequestMapping("/del/{name}")
    public String del(@PathVariable("name") String name){
        posService.delete(name);
        return "redirect:/list";
    }

    @RequestMapping("/sel")
    public String sel(String name ,Model model){
        List<String> list=posService.selectAllPosition();
        boolean x=list.contains(name);
        if(x){
            String s=posService.selectName(name);
            model.addAttribute("pos",s);
            return "emp/list1";
        }
        return "redirect:/list";
    }




}
